package com.cskaoyan.wordmemorize.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cskaoyan.wordmemorize.aop_whh.annotation.OperateLog;
import com.cskaoyan.wordmemorize.common.constant.ResultCodeEnum;
import com.cskaoyan.wordmemorize.common.exception.BusinessException;
import com.cskaoyan.wordmemorize.converter.GoodsConverter;
import com.cskaoyan.wordmemorize.dao.entity.GoodsDO;
import com.cskaoyan.wordmemorize.dao.mapper.GoodsMapper;
import com.cskaoyan.wordmemorize.dto.admin.DeleteGoodsOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.GoodsDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.GoodsCommand;
import com.cskaoyan.wordmemorize.request.GoodsCommandNew_whh;
import com.cskaoyan.wordmemorize.request.GoodsPageRequest;
import com.cskaoyan.wordmemorize.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, GoodsDO> implements GoodsService {

    @Autowired
    GoodsConverter goodsConverter;

    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public PageDTO<GoodsDTO> getGoodsPage(GoodsPageRequest request) {
        if (request == null) {
            throw new BusinessException(ResultCodeEnum.FAIL);
        }
        //       进行key条件查询
        LambdaQueryWrapper<GoodsDO> wrapper = new LambdaQueryWrapper<GoodsDO>()
                .like(request.getKeyword() != null, GoodsDO::getName, request.getKeyword())
                .in(request.getTypes() != null, GoodsDO::getType, request.getTypes());

        if (request.getPageNum() == null) {
            // 不分页
            List<GoodsDO> goodsDOS = goodsMapper.selectList(wrapper);
            return goodsConverter.goodsPage2PageDTO(goodsDOS, (long) goodsDOS.size());

        }

        // 分页
        Page<GoodsDO> page = new Page<>(request.getPageNum(), request.getCount());
        Page<GoodsDO> goodsDOPage = goodsMapper.selectPage(page, wrapper);

        //得到所有的数据
        List<GoodsDO> goodsList = goodsDOPage.getRecords();
        long total = goodsDOPage.getTotal();
        return goodsConverter.goodsPage2PageDTO(goodsList, (long) total);
    }

    @Override
    public void addGoods(GoodsCommand command) {

    }


    @OperateLog(dateType = "商品", operateType = 1)
    @Override
    public void addGoods(GoodsCommandNew_whh command) {
        GoodsDO goodsDO = goodsConverter.commandNew2GoodsDO(command);
        boolean save = save(goodsDO);
        if (!save) {
            throw new BusinessException(ResultCodeEnum.FAIL);
        }

    }

    @Override
    public void updateGoods(GoodsCommand command) {

    }

    @OperateLog(dateType = "商品", operateType = 1)
    @Override
    public void updateGoods(GoodsCommandNew_whh command) {
        GoodsDO goodsDO = goodsConverter.commandNew2GoodsDO(command);
        boolean result = this.updateById(goodsDO);
        if (!result) {
            throw new BusinessException(ResultCodeEnum.FAIL);
        }
    }

    @OperateLog(dateType = "商品", operateType = 2)
    @Override
    public DeleteGoodsOperateDTO deleteGoods(Long goodsId) {
        boolean result = removeById(goodsId);
        if (!result) {
            throw new BusinessException(ResultCodeEnum.FAIL);
        }
        GoodsDO goodsDO = getById(goodsId);
        return goodsConverter.goodsDO2DeleteGoodsDTO(goodsDO);

    }

    @OperateLog(dateType = "商品", operateType = 3)
    @Override
    public List<DeleteGoodsOperateDTO> deleteGoodsSelected(BatchDeleteCommand command) {
        if (command == null) {
            throw new BusinessException(ResultCodeEnum.FAIL);
        }
        removeByIds(command.getIds());
        List<GoodsDO> goodsDOS = listByIds(command.getIds());
        return goodsConverter.goodsDOs2DeleteGoodsDTOs(goodsDOS);

    }

    @OperateLog(dateType = "商品", operateType = 5)
    @Override
    public void updateGoodsStatus(Long goodsId, Integer status) {
        GoodsDO goodsDO = new GoodsDO();
        goodsDO.setId(goodsId);
        goodsDO.setStatus(status);
        boolean result = updateById(goodsDO);
        if (!result) {
            throw new BusinessException(ResultCodeEnum.FAIL);
        }

    }
}
